home *** CD-ROM | disk | FTP | other *** search
- /* spacerot.c bru 03/02/1986 17:06:23 */
- /* stdio.h */
- typedef int FILE;
-
- #define stdin 0
- #define stdout 1
- #define stderr 2
- #define stdprn 4
-
- #define NULL 0
- #define TRUE 1
- #define FALSE 0
- #define EOF (-1)
- #define ERR (-1)
-
- #include <math.h>
-
- #define LOCAL auto
- #define COORSIZE 4536
- #define pi 3.14159 /*used to convert degrees to radians*/
-
- /* structure of coordinate database */
- struct { int rec_c;
- char color_c;
- char size_c;
- int x_raw;
- int y_raw;
- int z_raw;
- char tic_one;
- char tic_two; } *starcoor;
-
- /* global variables */
- float d_x,d_y,d_z; /* display coordinates of viewed objects */
- int rot_x,rot_y,rot_z; /* rotation in degrees for a normal vector */
- int ship_x,ship_y,ship_z; /* current ship coordinates */
- double trans_x[4][4]; /* x coordinate transformation array */
- double trans_y[4][4]; /* y coordinate transformation array */
- double trans_z[4][4]; /* z coordinate transformation array */
- char *bcoor; /* beginning of coordinate file */
- char *ecoor; /* end of coordinate file */
- unsigned page,color;
- int backxy[100][2]; /* list of background stars */
- int blackxy[2][100][3]; /* page,stars,xy coord, dia, for blacking out */
-
- main()
- {
- init_var(); /* initalize var, load arrays + memory */
- draw_stars(); /* put current set of stars on screen */
- /* a couple of trial loops */
- for (rot_x = 1; rot_x <= 245; x_rot_inc(10) ) draw_stars();
- draw_stars();
- draw_stars();
- draw_stars();
- for (rot_y = 1; rot_y <= 245; y_rot_inc(10) ) draw_stars();
- draw_stars();
- /*for (rot_z = 1; rot_z <= 245; z_rot_inc(50) ) draw_stars();
- draw_stars();
- */ /* ending stuff */
- show_buffer(0);
- set_write_buffer(0);
- clear_color();
- set_video_mode(0x03);
-
- }
-
- void init_var() /* initialize variables, build arrays */
- {
- load_coor();
- set_video_mode(0x10);
- page = 0;
- init_backstars();
- ship_x = 1;
- ship_y = 1;
- ship_z = 1;
- rot_x = 10;
- rot_y = 45;
- rot_z = 90;
- x_rot_inc(0);
- y_rot_inc(0);
- z_rot_inc(0);
- }
-
- /* load coordinates, initialize pointers */
- void load_coor()
- {
- LOCAL char *cp;
- LOCAL int *fp;
- starcoor = bcoor = malloc ( COORSIZE );
- ecoor = bcoor + COORSIZE ;
- fp = fopen( "starcoor.dat", "r" );
- cp = bcoor;
- while ( cp != ecoor )
- {
- /* problem here, if 26 is fetched, a 255 is put */
- *cp = getc( fp );
- ++cp;
- }
- fclose("starcoor.dat");
- }
-
- void init_backstars() /* builds array of background stars */
- {
- srand(89);
- int srow,scol,i;
- double drow,dcol,dval;
- color = 8;
- for(i=0;i<100;i++)
- {
- dval = frand();
- drow = 350 * dval;
- dval = frand();
- dcol = 640 * dval;
- srow = (int)(drow);
- scol = (int)(dcol);
- backxy[i][0]=scol-320;
- backxy[i][1]=srow-175;
- }
- }
-
- void draw_backstars() /* draws the background stars */
- {
- int i;
- color = 8;
- for(i=0;i<100;i++) plotxy(backxy[i][0],backxy[i][1]);
- }
-
- void page_black() /* clear the page to black */
- {
- int urb;
- color = 0;
- for (urb=0;urb<100;urb++)circle(blackxy[page][urb][0],
- blackxy[page][urb][1],
- blackxy[page][urb][2]);
- }
-
- void transform_xyz() /* transforms coordinates of viewed object */
- {
- /* raw coord minus ships position divided by scaling factor */
- d_x = (starcoor.x_raw - ship_x)/.5;
- d_y = (starcoor.y_raw - ship_y)/.5;
- d_z = (starcoor.z_raw - ship_z)/.5;
- /* initialize global variables */
- /* printf("Original Coord %d, %d, %d Transformed coord %d, %d, %d\n",
- starcoor.x_raw,starcoor.y_raw,starcoor.z_raw,d_x,d_y,d_z); */
- }
-
- /* x_rot_inc increment x axis rotation */
- /* acts on global rot_x, and always returns 0-360 degrees of rotation */
- x_rot_inc(i)
- int i; /* amount of rotation in degrees */
- {
- double sx,cx; /* sine and cosine values */
- double frx; /* float values in radians */
- rot_x = rot_x + i;
- if (rot_x >= 360) rot_x = rot_x - 360;
- if (rot_x < 0) rot_x = rot_x +360;
- /* fix up sin and cosine values */
- /* convert degrees to radians */
- frx=pi*(rot_x/180.0);
- /* calculate cosines and sines */
- cx=cos(frx);
- sx=sin(frx);
- /* printf("X DegRot %d, RadRot %f, CosX %f, SinX %f\n",rot_x,frx,cx,sx); */
- /* fixup transformation matrix */
- trans_x[1][1]=1.0;
- trans_x[1][2]=0.0;
- trans_x[1][3]=0.0;
- trans_x[2][1]=0.0;
- trans_x[2][2]=cx;
- trans_x[2][3]=-sx;
- trans_x[3][1]=0.0;
- trans_x[3][2]=sx;
- trans_x[3][3]=cx;
- }
-
- /* y_rot_inc increment y axis rotation */
- /* acts on global rot_y, and always returns 0-360 degrees of rotation */
- y_rot_inc(i)
- int i; /* amount of rotation in degrees */
- {
- double sy,cy; /* sine and cosine values */
- double fry; /* float values in radians */
- rot_y = rot_y + i;
- if (rot_y >= 360) rot_y = rot_y - 360;
- if (rot_y < 0) rot_y = rot_y +360;
- /* fix up sin and cosine values */
- /* convert degrees to radians */
- fry=pi*(rot_y/180.0);
- /* calculate cosines and sines */
- cy=cos(fry);
- sy=sin(fry);
- /* printf("Y DegRot %d, RadRot %f, CosY %f, SinY %f\n",rot_y,fry,cy,sy); */
- /*fixup transformation matrix*/
- trans_y[1][1]=cy;
- trans_y[1][2]=0.0;
- trans_y[1][3]=sy;
- trans_y[2][1]=0.0;
- trans_y[2][2]=1.0;
- trans_y[2][3]=0.0;
- trans_y[3][1]=-sy;
- trans_y[3][2]=0.0;
- trans_y[3][3]=cy;
- }
-
- /* z_rot_inc increment z axis rotation */
- /* acts on global rot_z, and always returns 0-360 degrees of rotation */
- z_rot_inc(i)
- int i; /* amount of rotation in degrees */
- {
- double sz,cz; /* sine and cosine values */
- double frz; /* float values in radians */
- rot_z = rot_z + i;
- if (rot_z >= 360) rot_z = rot_z - 360;
- if (rot_z < 0) rot_z = rot_z +360;
- /* fix up sin and cosine values */
- /* convert degrees to radians */
- frz=pi*(rot_z/180.0);
- /* calculate cosines and sines */
- cz=cos(frz);
- sz=sin(frz);
- /* printf("Z DegRot %d, RadRot %f, CosZ %f, SinZ %f\n",rot_z,frz,cz,sz); */
- /*setup transformation matrix*/
- trans_z[1][2]=-sz;
- trans_z[1][3]=0.0;
- trans_z[2][1]=sz;
- trans_z[2][2]=cz;
- trans_z[2][3]=0.0;
- trans_z[3][1]=0.0;
- trans_z[3][2]=0.0;
- trans_z[3][3]=1.0;
- }
-
- /* x_rot.c rotates point coordinates about the x-axis */
- void x_rot()
- {
- double xx,yy,zz; /* temporary x,y,z coordinates */
- /* multiply coordinates by transformations */
- xx = (trans_x[1][1]*d_x) + (trans_x[1][2]*d_y) + (trans_x[1][3]*d_z);
- yy = (trans_x[2][1]*d_x) + (trans_x[2][2]*d_y) + (trans_x[2][3]*d_z);
- zz = (trans_x[3][1]*d_x) + (trans_x[3][2]*d_y) + (trans_x[3][3]*d_z);
- /* fix up the global coodinates of the viewed object */
- d_x = (float)(xx);
- d_y = (float)(yy);
- d_z = (float)(zz);
- return;
- }
-
- /* y_rot.c rotates point coordinates about the y-axis */
- void y_rot()
- {
- double xx,yy,zz; /* temporary x,y,z coordinates */
- /*multiply coordinates by transformations*/
- xx = (trans_y[1][1]*d_x) + (trans_y[1][2]*d_y) + (trans_y[1][3]*d_z);
- yy = (trans_y[2][1]*d_x) + (trans_y[2][2]*d_y) + (trans_y[2][3]*d_z);
- zz = (trans_y[3][1]*d_x) + (trans_y[3][2]*d_y) + (trans_y[3][3]*d_z);
- d_x=(float)(xx);
- d_y=(float)(yy);
- d_z=(float)(zz);
- return;
- }
-
- /* z_rot.c rotates point coordinates about the z-axis */
- void z_rot()
- {
- double xx,yy,zz; /* temporary x,y,z coordinates */
- /*multiply coordinates by transformations*/
- xx = (trans_z[1][1]*d_x) + (trans_z[1][2]*d_y) + (trans_z[1][3]*d_z);
- yy = (trans_z[2][1]*d_x) + (trans_z[2][2]*d_y) + (trans_z[2][3]*d_z);
- zz = (trans_z[3][1]*d_x) + (trans_z[3][2]*d_y) + (trans_z[3][3]*d_z);
- d_x=(float)(xx);
- d_y=(float)(yy);
- d_z=(float)(zz);
- return;
- }
-
- void draw_stars() /* draw the stars */
- {
- int diameter;
- /* set buffers */
- if (page == 0)
- {
- show_buffer(0);
- set_write_buffer(1);
- page = 1;
- }else{
- color = 8;
- plotxy(0,-1);
- plotxy(0,-2);
- plotxy(0,0);
- plotxy(0,1);
- plotxy(0,2);
- plotxy(-2,0);
- plotxy(-1,0);
- plotxy(1,0);
- plotxy(2,0);
- show_buffer(1);
- set_write_buffer(0);
- page = 0;
- }
- /* blacken a new page, put background stars in place */
- page_black();
- draw_backstars();
- starcoor = bcoor;
- int bru;
- bru = 0;
- while (starcoor < ecoor)
- {
- transform_xyz(); /* change positions relative to ships position */
- /* printf(" Coord before rotation %d, %d, %d\n",d_x,d_y,d_z);
- */ x_rot();
- y_rot();
- z_rot();
- /* printf(" Coord after rotation %d, %d, %d\n",d_x,d_y,d_z);
- */ /* don't bother with what you can't see anyway */
- if (d_z >= 0 && d_y > -175 && d_y < 175 && d_x > -320 && d_x < 320 )
- {
- color = 4;
- diameter = 8;
- if ( starcoor.color_c == 77 ) color = 4;
- if ( starcoor.color_c == 79 ) color = 1;
- if ( starcoor.color_c == 75 ) color = 6;
- if ( starcoor.color_c == 65 ) color = 7;
- if ( starcoor.color_c == 66 ) color = 9;
- if ( starcoor.color_c == 70 ) color = 10;
- if ( starcoor.color_c == 71 ) color = 14;
- if ( starcoor.size_c == 49 ) diameter = 8;
- if ( starcoor.size_c == 50 ) diameter = 7;
- if ( starcoor.size_c == 51 ) diameter = 6;
- if ( starcoor.size_c == 52 ) diameter = 5;
- if ( starcoor.size_c == 53 ) diameter = 4;
- if ( starcoor.size_c == 54 ) diameter = 3;
- /* printf(" To Circle x %d, y %d, radius %d\n",d_x,d_y,diameter);
- */ if ( starcoor.size_c == 55 ) diameter = 2;
- int dx,dy;
- dx=(int)(d_x);
- dy=(int)(d_y);
- circle(dx,dy,diameter);
- blackxy[page][bru][0] = dx;
- blackxy[page][bru][1] = dy;
- blackxy[page][bru][2] = diameter;
- bru++;
- }
- starcoor++;
- }
- }
-
- circle(xc,yc,radius) /* draws a circle */
- int xc,yc,radius;
- {
- /* printf(" in CIRCLE x %d, y %d, radius %d\n",xc,yc,radius);
- */ if (radius > 0)
- {
- int xcd,ycd,a,b,f;
- xcd=radius;
- ycd=0;
- a=-2*(xcd+1);
- b=1;
- f=0;
- do{
- /* printf(" p8 xc=%d, yc=%d, xcrd=%d, ycrd=%d\n",xc,yc,xcd,ycd);
- */ plotxy( ycd+xc,((5* xcd)/6)+yc); /* b quart c-clockwise */
- plotxy(-ycd+xc,((5* xcd)/6)+yc); /* b quart clockwise */
- plotxy(-ycd+xc,((5*-xcd)/6)+yc); /* d quart c-clockwise */
- plotxy( ycd+xc,((5*-xcd)/6)+yc); /* d quart clockwise */
- plotxy( xcd+xc,((5*-ycd)/6)+yc); /* a quart c-clockwise */
- plotxy( xcd+xc,((5* ycd)/6)+yc); /* a quart clockwise */
- plotxy(-xcd+xc,((5* ycd)/6)+yc); /* c quart c-clockwise */
- plotxy(-xcd+xc,((5*-ycd)/6)+yc); /* c quart clockwise */
- ycd=ycd+1;
- f=f+b;
- if ( f > radius )
- {
- f=f+a;
- a=a+2;
- xcd=xcd-1;
- }
- b=b+2;
- }
- while(b<=-a);
- }
- }
-
- plotxy(pcol,prow) /* plots points on the screen */
- int pcol,prow;
- {
- /* printf(" in plot px %d, py %d",pcol,prow);
- */ int ncol,nrow;
- unsigned ux,uy;
- ncol = pcol + 320; /* change from screen center 0,0 to */
- nrow = prow + 175; /* lower left 0,0 coordinates */
- /* printf(" calcu (+320)ncol %d, (+175)nrow %d\n",ncol,nrow);
- */ if ( nrow <= 349 && nrow >= 0 && ncol >= 0 && ncol <= 639 )
- {
- set_color(color);
- ux=(unsigned)(ncol);
- uy=(unsigned)(nrow);
- /* printf(" plotting ncol %d, nrow %d\n",ncol,nrow);
- */ c_write_dot(ux,uy);
- }
- }
-